import pymssql


class Save_database(object):
    def __init__(self, db):
        self.db = db
        self.cursor = self.db.cursor()

    def is_save_sku(self, sku, week):
        Flag = True
        try:
            sql = 'select sku from Paterson_Product_List where sku = %s and Update_week = %s'
            self.cursor.execute(sql, (sku, week))
            self.cursor.fetchall()
            Flag = False
        except Exception:
            Flag = True
        finally:
            return Flag

    def save_outter_data(self, sku, spu, pid, product_url, sale_price, product_title, comment_num, shop_name, shop_url, \
                      shop_promotion, first_category, second_category, week):
        if self.is_save_sku(sku, week):
            print(sku, spu, pid, product_url, sale_price, product_title, comment_num, shop_name, shop_url, \
                          shop_promotion, first_category, second_category, week)
            try:
                sql = 'insert into Paterson_Product_List(sku, spu, pid, product_url, sale_price, product_title, comment_num, \
                shop_name,shop_url, shop_promotion, first_category, second_category, update_week) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) '
                self.cursor.execute(sql, (sku, spu, pid, product_url, sale_price, product_title, comment_num, shop_name, shop_url, \
                                          shop_promotion, first_category, second_category, week))
                self.db.commit()
            except Exception as e:
                print('入库失败'+str(e))
                self.db.rollback()

    def save_innert_data(self, sku, Theme, P_price, J_Fans_price, J_xsth_sale, J_plus_price, J_user_price, Classification_list\
                    , Comment_count, Product_brand, Detailed_information, Shop_name, Shop_star, Commodity_evaluation\
                    , Logistics_evaluation, After_sales_evaluation, Product_category, Week):
        print(sku, Theme, P_price, J_Fans_price, J_xsth_sale, J_plus_price, J_user_price, Classification_list\
                , Comment_count, Product_brand, Detailed_information, Shop_name, Shop_star, Commodity_evaluation\
                 , Logistics_evaluation, After_sales_evaluation, Product_category)
        try:
            sql = 'insert into Paterson_Detailed_Information(sku, Theme, P_price, J_Fans_price, J_xsth_sale, J_plus_price, J_user_price, Classification_list\
                , Comment_count, Product_brand, Detailed_information, Shop_name, Shop_star, Commodity_evaluation\
                 , Logistics_evaluation, After_sales_evaluation, Product_category, Update_week) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
            self.cursor.execute(sql, (sku, Theme, P_price, J_Fans_price, J_xsth_sale, J_plus_price, J_user_price, Classification_list\
                    , Comment_count, Product_brand, Detailed_information, Shop_name, Shop_star, Commodity_evaluation\
                    , Logistics_evaluation, After_sales_evaluation, Product_category, Week))
            self.db.commit()
        except Exception as e:
            print(e)
            self.db.rollback()

    def save_quession_data(self, productId, quesion, answer, answer_create_date, answer_modified_date, answer_user, week, quession_id):
        print(productId, quesion, answer, answer_create_date, answer_modified_date, answer_user, week, quession_id)
        try:
            sql = 'insert into Paterson_Answer(sku, Quession, Answer, Answer_Creat_date, Answer_Modified_date, Asnwer_User, Update_week, Quession_id) values(%s,%s,%s,%s,%s,%s,%s,%s)'
            self.cursor.execute(sql, (int(productId), quesion, answer, answer_create_date, answer_modified_date, answer_user, week), quession_id)
            self.db.commit()
        except Exception as e:
            print(e)
            self.db.rollback()
